package com.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseManager 
{
	public static final String TEAM_NAME = "name";
	public static final String TEAM_POINTS = "points";
	public static final String MATCHES_WON = "won";
	public static final String MATCHES_DRAWN = "drawn";
	public static final String MATCHES_LOST = "lost";
	public static final String GS = "gs";
	public static final String GA = "ga";
	//public static final String PLAYED = "played";
	public static final String KEY_ROWID = "_id";//convention to have "_" before
	public static final String TAG = "DatabaseManager";
	private SQLiteDatabase mDB;
	private final static String DB_TABLE = "Standings";
	private static final String DATABASE_CREATE = "create table "+DB_TABLE
									+" (_id integer primary key autoincrement,"
									+TEAM_NAME+" text not null,"
									+TEAM_POINTS+" text,"
									+MATCHES_WON+" text,"
									+MATCHES_DRAWN+" text,"
									+MATCHES_LOST+" text,"
									+GS+" text,"
									+GA+" text);";
	private final static String DB_NAME = "League";
	private static final int DB_VERSION = 1;
	private final Context mCtx;
	private DatabaseHelper mDBHelper;
	
	public DatabaseManager(Context ctx)
	{
		this.mCtx = ctx;

	}
	
	
	public DatabaseManager open() throws SQLException
	{
		 mDBHelper = new DatabaseHelper(mCtx);
		 mDB = mDBHelper.getWritableDatabase();
		 return this;
	}
		
	public void close()
	{
		mDBHelper.close();
	}
	
	public boolean deleteRow(long rowID)
	{
		return mDB.delete(DB_TABLE, KEY_ROWID + "=" +rowID, null)>0;//mDB.delete returns how many rows it has returned.
	}
	
	
	public long createMatchTableWithValues(String name, String points, String won, String drawn, String lost, String gs,String ga){
		ContentValues content = new ContentValues();
		content.put(TEAM_NAME, name);
		content.put(TEAM_POINTS, points);
		content.put(MATCHES_WON, won);
		content.put(MATCHES_DRAWN, drawn);
		content.put(MATCHES_LOST, lost);
		//content.put(PLAYED, played);
		content.put(GS, gs);
		content.put(GA, ga);
		return mDB.insert(DB_TABLE, null, content);		
	} 
	
	
	
	public Cursor getTeamCursor(long rowId){
		Cursor mCursor = mDB.query(true, DB_TABLE, new String[]{TEAM_NAME, TEAM_POINTS, MATCHES_WON,MATCHES_DRAWN,MATCHES_LOST,GS,GA}, KEY_ROWID+"="+rowId,null,null,null,null,null);
		if(mCursor != null)
			mCursor.moveToFirst();
		
		return mCursor;

	}
	
	public Cursor getAllTeamsCursor(){
		Cursor mCursor = mDB.query(true, DB_TABLE, new String[]{TEAM_NAME, TEAM_POINTS, MATCHES_WON,MATCHES_DRAWN,MATCHES_LOST,GS,GA},null,null,null,null,null,null);
		if(mCursor != null)
			mCursor.moveToFirst();
		
		return mCursor;

	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		DatabaseHelper(Context context)
		{
			super(context, DB_NAME , null, DB_VERSION);
			
		}

		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) 
		{
			Log.v(TAG," Database Version is being updated, all data will be lost");
			db.execSQL("DROP TABLE IF EXITS "+DB_TABLE);
			onCreate(db);			
		}
	}
	
}
